<?php
namespace app\admin\controller;
use app\common\controller\AdminController;
use chrome\ChromePhp;
use common\Common;
use uploadImg\UtilImage;
use app\common\model\weixin\LibResponse;
use wechatUtil\Menu;

/**
 * 微信智能回复
 * @name WeixinResponse.php
 * @package cws
 * @category controller
 * @link http://www.chanekeji.com
 * @author jianfang
 * @version 2.0
 * @copyright CHANGE INC
 * @since 2016-08-13
 */
class  WeixinResponse extends AdminController{
	private $lib_response;
	
	/**
	 * 构造函数
	 */
	function __construct() {
		parent::__construct ();
		error_reporting ( E_ALL & ~ E_NOTICE & ~ E_DEPRECATED & ~ E_STRICT & ~ E_WARNING );
		$this->rightVerify(session('admin'), url("admin/Login/login"));
		$this->lib_response = new LibResponse();
	}
	
	/**
	 * 智能回复页面
	 */
	function responsePage(){
		$this->getSetMenu($this);
		$useResult =  $this->lib_response->fileConfigObj->getFileConfigValue('is_use');
		$this->assign('isUse',$useResult['data']);//智能回复功能开关标识，1 启用，0 停用
		if($useResult['data'] ){
			$messageResult = $this->lib_response->findMessage(array('use_scene'=>0));//被关注自动回复
			if($messageResult['data']['type'] == 'my_news' && $messageResult['data']['content'] != ''){
				$messageResult['data']['content'] = json_encode($messageResult['data']['content'],true);
			}
			$this->assign('attentionMessage',$messageResult['data']);
			$messageResult = $this->lib_response->findMessage(array('use_scene'=>1));//统一回复
			if($messageResult['data']['type'] == 'my_news' && $messageResult['data']['content'] != ''){
				$messageResult['data']['content'] = json_encode($messageResult['data']['content'],true);
			}
			$this->assign('allMessage',$messageResult['data']);			
			$messageResult = $this->lib_response->findAllRule();//关键词回复
			if($messageResult['data']['type'] == 'my_news' && $messageResult['data']['content'] != ''){
				$messageResult['data']['content'] = json_encode($messageResult['data']['content'],true);
			}
			$this->assign('keywordsMessage',$messageResult['data']);
		}
		//对应页面的某个导航模块 attentionMessage，allMessage，keywordsMessage
		$navFlag = input("param.navFlag");
		$this->assign('navFlag',$navFlag);
		$this->log(__CLASS__, __FUNCTION__, "智能回复页面", 1, 'view');
		return $this->fetch("WeixinResponse/responsePage/responsePage");
	}
	
	/**
	 * 启用停用智能回复
	 */
	function useResponse(){
		$responseConfig['is_use'] =  input('param.is_use');
		$result = $this->lib_response->fileConfigObj->setFileKeyValue('is_use', input('param.is_use'));
		$this->log(__CLASS__, __FUNCTION__, "启用停用智能回复", 0, 'edit');
		echo json_encode($result);
	}
	
	/**
	 * 修改回复消息
	 */
	function updateMessage(){
		$messageInfo['type'] = input('param.type');//text文字image图片news图文连接voice音频vedio视频
		$messageInfo['content'] = input('param.content');
		$messageInfo['media_id'] = input('param.media_id');
		$messageInfo['url'] = input('param.url');
		$messageInfo['use_scene'] = input('param.use_scene');
		if(input('param.id')){
			$result  = $this->lib_response->updateMessage(array('id'=>input('param.id')), $messageInfo);
		}else{
			$result  = $this->lib_response->addMessage($messageInfo);
		}
		$this->log(__CLASS__, __FUNCTION__, "修改回复消息", 0, 'edit');
		echo json_encode($result);
	}
	
	/**
	 * 启用停用消息
	 */
	function useMessage(){
		$messageInfo['is_use'] = input('param.is_use');
		$result  = $this->lib_response->updateMessage(array('id'=>input('param.id')), $messageInfo);
		$this->log(__CLASS__, __FUNCTION__, "启用停用消息", 0, 'edit');
		echo json_encode($result);
	}
	
	/**
	 * 添加回复规则
	 */
	function addRule(){
		//规则信息
		$ruleInfo['rule_name'] = input('param.rule_name');
		$ruleInfo['keywords'] = input('param.keywords');
		$ruleInfo['is_use'] = input('param.is_use');
		//消息信息
		$messageInfo['type'] = input('param.type');//text文字image图片news图文连接voice音频vedio视频
		$messageInfo['content'] = input('param.content');
		$messageInfo['media_id'] = input('param.media_id');
		$messageInfo['url'] = input('param.url');
		$messageInfo['is_use'] = input('param.is_use');
		$result  = $this->lib_response->addRule($ruleInfo, $messageInfo);
		$this->log(__CLASS__, __FUNCTION__, "添加回复规则", 0, 'add');
		echo json_encode($result);
	}
	
	/**
	 * 修改回复规则
	 */
	function updateRule(){
		//规则基本信息
		$ruleInfo['rule_name'] = input('param.rule_name');
		$ruleInfo['keywords'] = input('param.keywords');
		$ruleInfo['is_use'] = input('param.is_use');
		$ruleInfo['message_id'] = input('param.message_id');
		//回复消息信息
		$messageInfo['type'] = input('param.type');//text文字image图片news图文连接voice音频vedio视频
		$messageInfo['content'] = input('param.content');
		$messageInfo['media_id'] = input('param.media_id');
		$messageInfo['url'] = input('param.url');
		$result  = $this->lib_response->updateRule(array('id'=>input('param.id')), $ruleInfo,$messageInfo);
		$this->log(__CLASS__, __FUNCTION__, "修改回复规则", 0, 'edit');
		echo json_encode($result);
	}
	
	/**
	 * 启用停用规则
	 */
	function useRule(){
		$ruleInfo['is_use'] = input('param.is_use');
		$result  = $this->lib_response->updateRuleJust(array('id'=>input('param.id')), $ruleInfo);
		$this->log(__CLASS__, __FUNCTION__, "启用停用回复规则", 0, 'edit');
		echo json_encode($result);
	}

	/**
	 ** 删除规则
	 */
	 function deleteRule(){
	    $result  = $this->lib_response->deleteRule(input('param.id'));
	    $this->log(__CLASS__, __FUNCTION__, "删除回复规则", 0, 'del');
        echo json_encode($result);
	 }
	 
	 /**
	  * 上传自定义图文图片
	  */
	 function uploadMyMessageImage(){ 
	     $prevUrl = input('param.prevUrl');
	     if($prevUrl){
	      unlink($prevUrl);   
	     }
	     $url = UtilImage::uploadPhotoJust('imgurl', 'upload/image/weixin/myNews/'.date('Ymd'));
	     if($url){
	         echo json_encode(Common::errorArray(0, "上传成功", $url));
	     }else{
	         echo json_encode(Common::errorArray(1, "上传失败", false));
	     }
	 }
	 
	 /**
	  * 删除自定义图文图片
	  */
	 function deleteMyMessageImage(){
	     $picUrl = input('param.picUrl');
	     if(file_exists($picUrl)){
	         if(unlink($picUrl)){
	             echo json_encode(Common::errorArray(0, "删除成功", true));
	         }else{
	             echo json_encode(Common::errorArray(1, "删除失败", false));
	         }
	     }else{
	         echo json_encode(Common::errorArray(0, "文件不存在", true));
	     }
	 }
}